package com.zynn.api.yinian.zuul.util;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.context.RequestContext;
import com.zynn.common.pojo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;


/**
 * @author jerry-liqi
 *
 * zull 工具类
 */
@Slf4j
public class ZuulUtil {

    /**
     * 获取路由之后的返回值
     *
     * @param currentContext 当前的上下文
     * @return result
     */
    public static Result getResponseResult(RequestContext currentContext) {
        //获得输入流
        InputStream stream = currentContext.getResponseDataStream();
        String jsonResult = "";
        try {
            //根据流读取返回的json字符串
            jsonResult = StreamUtil.readFromStream(stream);
        } catch (Exception e) {
            log.error("ZuulUtil.getResponseResult", e);
        }
        //格式化之后转为Result对象后返回
        return StringUtils.isEmpty(jsonResult) ? null : JSON.parseObject(jsonResult, Result.class);
    }

}
